package edu.aysf.controller;

import edu.aysf.entity.GoodsEntity;
import edu.aysf.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    private  GoodsService GoodsService;

//    public GoodsController(GoodsService GoodsService) {
//        this.GoodsService = GoodsbService;
//    }

    // 查询所有商品
    @RequestMapping("/select/all")
    public List<GoodsEntity> selectGoods(){
        // 写法2
        return GoodsService.selectGoods();
    }

    // 根据id查询商品
    @RequestMapping("/select/id")
    public List<GoodsEntity> selectGoodsById(@RequestBody Map<String, Object> map){
//    public Map selectGoods(@RequestBody Map<String, Object> user){
        // 写法1
//        int id = (int)user.get("id");
//        List<GoodsEntity> list = GoodsService.selectGoodsById(id);
//        HashMap map = new HashMap();
//        map.put("list", list);
//        return map;

        // 写法2
        int id = Integer.parseInt(map.get("id").toString());
        return GoodsService.selectGoodsById(id);
    }


    // 查询商品单价
    @RequestMapping("/select/price")
    public Float selectGoodsPrice(@RequestBody Map<String,Object> map) {
        int id = Integer.parseInt(map.get("goods_id").toString());
        return GoodsService.selectGoodsPrice(id);
    }

    // 根据商品名查询商品
    @RequestMapping("/select/name")
    public List<GoodsEntity> selectGoodsByName(@RequestBody Map<String, Object> map) {
        String goods_name = map.get("name").toString();
        return GoodsService.selectGoodsByName(goods_name);
    }

    // 查询商品库存数量 ; feign调用时必须加 @RequestParam 或 其他注解
    @RequestMapping("/select/count")
    public int selectCount(@RequestBody Map<String,Object> map){
        // 写法2
        int id = Integer.parseInt(map.get("id").toString());
        return GoodsService.selectCount(id);
    }

    // 新增商品
    @RequestMapping("/create")
    public int createGoods(@RequestBody Map<String, Object> map){

//        Integer id = (Integer) map.get("id");
//        String goods_name = (String)map.get("goods_name");
//        Integer goods_count = (Integer) map.get("goods_count");
//        float goods_price = Float.parseFloat(map.get("goods_price").toString());
//        float goods_money = Float.parseFloat(map.get("goods_money").toString());

        Integer id = Integer.parseInt(map.get("id").toString());
        String goods_name = String.valueOf(map.get("goods_name"));
        if (goods_name.isEmpty()) {
            throw new IllegalArgumentException("Goods name cannot be empty");
        }
        Integer goods_count = Integer.parseInt(map.get("goods_count").toString());
        float goods_price = Float.parseFloat(map.get("goods_price").toString());
        float goods_money = Float.parseFloat(map.get("goods_money").toString());

        // 调用服务层的方法
        return GoodsService.createGoods(id,goods_name,goods_count,goods_price,goods_money);
    }
    @RequestMapping("/delete")
    public int deleteGoods(@RequestBody Map<String,Object> map){
        Integer goods_id = Integer.parseInt(map.get("id").toString());
        return GoodsService.deleteGoods(goods_id);
    }
    // 更新商品库存
    @RequestMapping("/update")
    public int updateGoods(@RequestBody Map<String, Object> map) {
        Integer goods_id = Integer.parseInt(map.get("id").toString());
        Integer goods_count = Integer.parseInt(map.get("goods_count").toString());
        float goods_price = Float.parseFloat(map.get("goods_price").toString());
        float goods_money = Float.parseFloat(map.get("goods_money").toString());
       return GoodsService.updateGoods(goods_id, goods_count,goods_price,goods_money);
    }

}
